<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elasticsearch全文搜索性能优化指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f9fafb;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
            color: white;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .icon-box {
            width: 48px;
            height: 48px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 2px;
            left: 0;
            width: 100%;
            height: 8px;
            background-color: rgba(79, 70, 229, 0.3);
            z-index: -1;
            transition: all 0.3s ease;
        }
        .highlight:hover::after {
            height: 14px;
            background-color: rgba(79, 70, 229, 0.5);
        }
        .step-number {
            font-size: 1.5rem;
            font-weight: 700;
            color: #4f46e5;
            margin-right: 1rem;
        }
        .divider {
            border-top: 1px solid #e5e7eb;
            position: relative;
        }
        .divider::before {
            content: '';
            position: absolute;
            top: -1px;
            left: 0;
            width: 80px;
            height: 2px;
            background-color: #4f46e5;
        }
        .tooltip:hover .tooltip-content {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        .tooltip-content {
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto">
            <div class="flex flex-col items-center text-center">
                <span class="inline-block px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm font-medium mb-4">性能优化指南</span>
                <h1 class="text-4xl sm:text-5xl font-bold mb-6 leading-tight">Elasticsearch全文搜索<br>性能优化全攻略</h1>
                <p class="text-xl max-w-3xl opacity-90 mb-8">掌握核心优化策略，提升搜索性能10倍以上</p>
                <div class="flex flex-wrap justify-center gap-4">
                    <a href="#index-design" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-lg hover:bg-opacity-90 transition-all">索引优化</a>
                    <a href="#query-optimization" class="px-6 py-3 bg-white bg-opacity-20 font-medium rounded-lg hover:bg-opacity-30 transition-all">查询优化</a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Intro -->
        <section class="mb-16">
            <div class="prose prose-indigo max-w-none">
                <p class="text-lg text-gray-700">优化 Elasticsearch 中的全文搜索性能是一个多方面的任务，涉及索引设计、查询优化、集群配置等多个领域。为了实现高效的全文搜索，开发者需要理解 Elasticsearch 的工作原理并采取一系列优化策略。</p>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16 bg-white rounded-xl shadow-md p-6">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">Elasticsearch性能优化全景图</h2>
            <div class="mermaid">
                graph TD
                    A[Elasticsearch性能优化] --> B[索引设计优化]
                    A --> C[查询优化]
                    A --> D[索引数据优化]
                    A --> E[集群配置优化]
                    A --> F[缓存机制]
                    A --> G[性能监控]
                    
                    B --> B1[分片与副本配置]
                    B --> B2[字段数据类型]
                    B --> B3[分析器选择]
                    
                    C --> C1[使用过滤器]
                    C --> C2[查询类型选择]
                    C --> C3[查询结构优化]
                    
                    D --> D1[字段映射优化]
                    D --> D2[减少数据量]
                    
                    E --> E1[硬件资源]
                    E --> E2[集群设置]
                    
                    F --> F1[查询缓存]
                    F --> F2[过滤器缓存]
                    
                    G --> G1[监控工具]
                    G --> G2[性能测试]
            </div>
        </section>

        <!-- Index Design -->
        <section id="index-design" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="step-number">1</div>
                <h2 class="text-3xl font-bold text-gray-800">索引设计优化</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Card 1 -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="icon-box bg-indigo-100 text-indigo-600 mb-4">
                            <i class="fas fa-chart-pie text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">选择合适的分片和副本配置</h3>
                        <p class="text-gray-600 mb-4">Elasticsearch 将索引的数据分成若干个分片（shard），并在集群中的各个节点之间分布这些分片。配置合理的分片数量可以提高搜索性能。</p>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span><span class="font-medium">分片数量</span>: 建议每个分片的大小保持在几十 GB 以下</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span><span class="font-medium">副本数量</span>: 增加副本数量可以提高查询吞吐量，但会增加写入延迟</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <!-- Card 2 -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="icon-box bg-purple-100 text-purple-600 mb-4">
                            <i class="fas fa-keyboard text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">使用适当的字段数据类型</h3>
                        <p class="text-gray-600 mb-4">Elasticsearch 支持多种字段数据类型，不同的数据类型在存储和搜索时的表现差异很大。</p>
                        <div class="bg-gray-50 rounded-lg p-4">
                            <div class="flex items-center justify-between mb-2">
                                <span class="font-medium">text 类型</span>
                                <span class="text-sm bg-indigo-100 text-indigo-800 px-2 py-1 rounded">全文搜索</span>
                            </div>
                            <div class="flex items-center justify-between">
                                <span class="font-medium">keyword 类型</span>
                                <span class="text-sm bg-purple-100 text-purple-800 px-2 py-1 rounded">精确匹配</span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Card 3 -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="icon-box bg-blue-100 text-blue-600 mb-4">
                            <i class="fas fa-search text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">选择合适的分析器</h3>
                        <p class="text-gray-600 mb-4">分析器在创建索引时将文本分解为词条，并在查询时对搜索词进行相同的处理。</p>
                        <div class="space-y-3">
                            <div class="tooltip relative inline-block">
                                <span class="cursor-pointer bg-blue-50 text-blue-700 px-3 py-1 rounded-full text-sm font-medium">standard 分析器</span>
                                <div class="tooltip-content absolute z-10 w-64 mt-2 p-3 bg-white shadow-lg rounded-lg border border-gray-200 text-sm">
                                    提供基本的词条分解和标准化功能，适用于大多数场景
                                </div>
                            </div>
                            <div class="tooltip relative inline-block">
                                <span class="cursor-pointer bg-indigo-50 text-indigo-700 px-3 py-1 rounded-full text-sm font-medium">自定义分析器</span>
                                <div class="tooltip-content absolute z-10 w-64 mt-2 p-3 bg-white shadow-lg rounded-lg border border-gray-200 text-sm">
                                    适用于需要高级文本分析功能的场景，如停用词过滤、词干提取等
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Divider -->
        <div class="divider my-12"></div>

        <!-- Query Optimization -->
        <section id="query-optimization" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="step-number">2</div>
                <h2 class="text-3xl font-bold text-gray-800">查询优化</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Card 1 -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="icon-box bg-green-100 text-green-600 mb-4">
                            <i class="fas fa-filter text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">使用过滤器代替查询</h3>
                        <p class="text-gray-600 mb-4">过滤器仅判断文档是否匹配某个条件而不计算相关性，可以显著提高性能。</p>
                        <div class="bg-gray-50 rounded-lg p-4">
                            <div class="mb-3">
                                <div class="flex items-center justify-between mb-1">
                                    <span class="font-medium">过滤器 (filter)</span>
                                    <span class="text-xs bg-green-100 text-green-800 px-2 py-0.5 rounded">高性能</span>
                                </div>
                                <p class="text-sm text-gray-500">不计算相关性评分，结果可缓存</p>
                            </div>
                            <div>
                                <div class="flex items-center justify-between mb-1">
                                    <span class="font-medium">查询 (query)</span>
                                    <span class="text-xs bg-yellow-100 text-yellow-800 px-2 py-0.5 rounded">计算密集型</span>
                                </div>
                                <p class="text-sm text-gray-500">计算相关性评分，结果不缓存</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Card 2 -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="p-6">
                        <div class="icon-box bg-yellow-100 text-yellow-600 mb-4">
                            <i class="fas fa-code text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3 text-gray-800">优化查询结构</h3>
                        <p class="text-gray-600 mb-4">简化查询结构可以提升性能，避免不必要的开销。</p>
                        <ul class="space-y-2 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-400 mt-1 mr-2"></i>
                                <span>减少嵌套查询和不必要的 <code class="bg-gray-100 px-1 rounded">bool</code> 子句</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-times-circle text-red-400 mt-1 mr-2"></i>
                                <span>避免使用通配符查询（如 <code class="bg-gray-100 px-1 rounded">*</code> 和 <code class="bg-gray-100 px-1 rounded">?</code>）</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>使用 <code class="bg-gray-100 px-1 rounded">prefix</code> 查询或 n-gram 索引替代通配符</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Other Sections -->
        <section class="grid md:grid-cols-3 gap-8 mb-16">
            <!-- Index Data Optimization -->
            <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="step-number">3</div>
                        <h3 class="text-xl font-bold text-gray-800">索引数据优化</h3>
                    </div>
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>优化字段映射（Mapping）</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>禁用不必要的 <code class="bg-gray-100 px-1 rounded">_source</code> 字段</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>减少字段数量和存储数据量</span>
                        </li>
                    </ul>
                </div>
            </div>
            
            <!-- Cluster Config -->
            <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="step-number">4</div>
                        <h3 class="text-xl font-bold text-gray-800">集群配置优化</h3>
                    </div>
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-memory text-blue-500 mt-1 mr-2"></i>
                            <span>增加内存和CPU资源</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-cog text-blue-500 mt-1 mr-2"></i>
                            <span>优化线程池设置</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-clock text-blue-500 mt-1 mr-2"></i>
                            <span>调整刷新间隔</span>
                        </li>
                    </ul>
                </div>
            </div>
            
            <!-- Caching -->
            <div class="card bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="flex items-center mb-4">
                        <div class="step-number">5</div>
                        <h3 class="text-xl font-bold text-gray-800">缓存机制</h3>
                    </div>
                    <div class="space-y-4">
                        <div>
                            <div class="flex items-center mb-1">
                                <i class="fas fa-database text-purple-500 mr-2"></i>
                                <span class="font-medium">查询缓存</span>
                            </div>
                            <p class="text-sm text-gray-500">缓存常见查询结果，提升重复查询性能</p>
                        </div>
                        <div>
                            <div class="flex items-center mb-1">
                                <i class="fas fa-filter text-purple-500 mr-2"></i>
                                <span class="font-medium">过滤器缓存</span>
                            </div>
                            <p class="text-sm text-gray-500">自动缓存过滤器结果，显著提升过滤查询性能</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Performance Monitoring -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="step-number">6</div>
                <h2 class="text-3xl font-bold text-gray-800">性能监控与调整</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6 md:p-8">
                    <div class="grid md:grid-cols-2 gap-8">
                        <div>
                            <h3 class="text-xl font-bold mb-4 text-gray-800">监控工具</h3>
                            <div class="flex items-center mb-3">
                                <div class="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-chart-line text-indigo-600"></i>
                                </div>
                                <span class="font-medium">Kibana 监控</span>
                            </div>
                            <div class="flex items-center mb-3">
                                <div class="w-8 h-8 rounded-full bg-green-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-tachometer-alt text-green-600"></i>
                                </div>
                                <span class="font-medium">Elasticsearch Monitoring</span>
                            </div>
                            <div class="flex items-center">
                                <div class="w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-server text-blue-600"></i>
                                </div>
                                <span class="font-medium">第三方监控工具</span>
                            </div>
                        </div>
                        
                        <div>
                            <h3 class="text-xl font-bold mb-4 text-gray-800">关键指标</h3>
                            <div class="space-y-3">
                                <div class="flex items-center">
                                    <div class="w-2 h-2 rounded-full bg-red-500 mr-3"></div>
                                    <span>CPU 使用率</span>
                                </div>
                                <div class="flex items-center">
                                    <div class="w-2 h-2 rounded-full bg-yellow-500 mr-3"></div>
                                    <span>内存使用率</span>
                                </div>
                                <div class="flex items-center">
                                    <div class="w-2 h-2 rounded-full bg-green-500 mr-3"></div>
                                    <span>查询延迟</span>
                                </div>
                                <div class="flex items-center">
                                    <div class="w-2 h-2 rounded-full bg-purple-500 mr-3"></div>
                                    <span>GC 频率</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- CTA -->
        <section class="bg-indigo-50 rounded-xl p-8 md:p-12 mb-16 text-center">
            <h2 class="text-2xl font-bold mb-4 text-gray-800">准备好优化您的Elasticsearch性能了吗？</h2>
            <p class="text-gray-600 mb-6 max-w-2xl mx-auto">立即应用这些优化策略，提升您的搜索系统性能，为用户带来更好的搜索体验。</p>
            <button class="px-8 py-3 bg-indigo-600 text-white font-medium rounded-lg hover:bg-indigo-700 transition-all">获取专业咨询</button>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors">
                        <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>